package com.ln.adoms.entity;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import com.ln.core.orm.hibernate.BaseEntity;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;

/**
 * 区域
 *
 * @author Felix
 */
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "parent"})
public class Region extends BaseEntity {
    //区域名
    private String name;
    //区域代码
    private String code;
    //父区域ID
    @ManyToOne
    @JoinColumn(name = "parent_id", nullable = true)
    private Region parent;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Region getParent() {
        return parent;
    }

    public void setParent(Region parent) {
        this.parent = parent;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDisplayRegionName() {
        StringBuilder sb = new StringBuilder();
        if (parent != null) {
            if (IsNotEmpty(parent.getName())) {
                sb.append(parent.getName() + "-");
            }
            if (parent.getParent() != null) {
                if (IsNotEmpty(parent.getParent().getName())) {
                    sb.insert(0, parent.getParent().getName() + "-");
                }
                if (parent.getParent().getParent() != null) {
                    if (IsNotEmpty(parent.getParent().getParent().getName())) {
                        sb.insert(0, parent.getParent().getParent().getName() + "-");
                    }
                }
            }
        }
        sb.append(name);
        return sb.toString();
    }

    private boolean IsNotEmpty(String name) {
        if (!"null".equals(name) && name != null && !"".equals(name.trim())) {
            return true;
        } else {
            return false;
        }
    }
}
